//
//  WelcomeViewController.swift
//  Weibo
//
//  Created by 明丽 on 16/1/15.
//  Copyright © 2016年 明丽. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelcomeViewController: UIViewController {
    
    //当页面还未加载完成时调用
    override func loadView() {
        view = backImageView
    }

 override  func viewDidAppear(animated: Bool) {
        super.viewWillAppear(animated)
    //执行动画效果,因为该动画会被频繁调用所以在这个方法中写在viewdidload重写只会被调用一次
    showAnimation()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //添加子视图
        setupUI()
    }
    
    private func setupUI() {
    self.view.addSubview(headImageView)
    self.view.addSubview(welcomeLabel)
    //设置约束
        //设置约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            make.centerX.equalTo(self.view.snp_centerX)
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(headImageView.snp_centerX)
        }
        //设置图像为圆的
        headImageView.layer.cornerRadius = 45
        headImageView.layer.masksToBounds = true
        //
        //隐藏文案
        welcomeLabel.alpha = 0
        //显示用户头像 
//    headImageView.sd_setImageWithURL(UserAccountViewController().headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
    headImageView.sd_setImageWithURL(UserAccountViewController().headImageURL, placeholderImage: UIImage(named:"avatar_default_big"))
    }

    //MAKE: 动画效果
  private  func showAnimation() {
    let offset = -UIScreen.mainScreen().bounds.height + 160

    //更新某一条已经添加过的约束,如果该约束不存在会自动添加该约束
    UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
        self.headImageView.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(offset)
        })
        self.view.layoutIfNeeded()
        
        }) { (_) -> Void in
           // print("OK")
            //显示文案
            UIView.animateWithDuration(0.6, animations: { () -> Void in
                self.welcomeLabel.alpha = 1.0
                }, completion: { (_) -> Void in
                    print("OK")
                    //像首页发出通知
                    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVC, object: nil)
            })
    
    }
    
    }
    
    
    
    //懒加载视图
    private lazy var backImageView: UIImageView =  UIImageView(image: UIImage(named: "ad_background"))
    //用户头像
    private lazy var headImageView: UIImageView = UIImageView()
    private lazy var welcomeLabel: UILabel = UILabel(title: (UserAccountViewController().userName ?? "") + " 欢迎归来", fontSize: 18, color: UIColor.darkGrayColor())
//        let l = UILabel()
//        l.text = (UserAccountViewController().userName ?? "") + "欢迎归来"
//        l.textColor = UIColor.darkGrayColor()
//        l.font = UIFont.systemFontOfSize(18)
//        l.sizeToFit()
//        return l
//    }()
//
    
}
